package org.jenkinsci.plugins.qywechat;

import org.jenkinsci.plugins.qywechat.model.NotificationConfig;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import hudson.model.AbstractProject;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import hudson.util.Secret;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import java.util.logging.Logger;

/**
 * 数据绑定
 * 
 * @author jiaju
 */
@Symbol("qyWechatNotification")
public class DescriptorImpl extends BuildStepDescriptor<Publisher> {
	private static final Logger logger = Logger.getLogger(DescriptorImpl.class.getName());
	
	// 配置信息
	private NotificationConfig config = new NotificationConfig();

	public DescriptorImpl() {
		super(QyWechatNotification.class);
		load();
	}

	public String getWebhookUrl() {
		return this.config.webhookUrl;
	}

	public void setWebhookUrl(String webhookUrl) {
		this.config.webhookUrl = webhookUrl;
	}

	public String getTopicName() {
		return this.config.topicName;
	}

	public void setTopicName(String topicName) {
		this.config.topicName = topicName;
	}

	public String getMentionedId() {
		return this.config.mentionedId;
	}

	public void setMentionedId(String mentionedId) {
		this.config.mentionedId = mentionedId;
	}

	public String getMentionedMobile() {
		return this.config.mentionedMobile;
	}

	public void setMentionedMobile(String mentionedMobile) {
		this.config.mentionedMobile = mentionedMobile;
	}

	public boolean isUseProxy() {
		return this.config.useProxy;
	}

	public void setUseProxy(boolean useProxy) {
		this.config.useProxy = useProxy;
	}

	public boolean isFailNotify() {
		return this.config.failNotify;
	}

	public void setFailNotify(boolean failNotify) {
		this.config.failNotify = failNotify;
	}

	public String getProxyHost() {
		return this.config.proxyHost;
	}

	public void setProxyHost(String proxyHost) {
		this.config.proxyHost = proxyHost;
	}

	public int getProxyPort() {
		return this.config.proxyPort;
	}

	public void setProxyPort(int proxyPort) {
		this.config.proxyPort = proxyPort;
	}

	public String getProxyUsername() {
		return this.config.proxyUsername;
	}

	public void setProxyUsername(String proxyUsername) {
		this.config.proxyUsername = proxyUsername;
	}

	public String getProxyPassword() {
		return Secret.toString(this.config.proxyPassword);
	}

	public void setProxyPassword(String proxyPassword) {
		this.config.proxyPassword = Secret.fromString(proxyPassword);
	}

	public boolean isFailSend() {
		return this.config.failSend;
	}

	public void setFailSend(boolean failSend) {
		this.config.failSend = failSend;
	}

	public boolean isSuccessSend() {
		return this.config.successSend;
	}

	public void setSuccessSend(boolean successSend) {
		this.config.successSend = successSend;
	}

	public boolean isAbortSend() {
		return this.config.abortSend;
	}

	public void setAboutSend(boolean aboutSend) {
		this.config.abortSend = aboutSend;
	}

	public boolean isUnstableSend() {
		return this.config.unstableSend;
	}

	public void setUnstableSend(boolean unstableSend) {
		this.config.unstableSend = unstableSend;
	}

	public boolean isStartBuild() {
		return this.config.startBuild;
	}

	public void setStartBuild(boolean startBuild) {
		this.config.startBuild = startBuild;
	}
	
	public void setPrintPushMsgOnBuildLog(boolean printPushMsgOnBuildLog) {
		this.config.printPushMsgOnBuildLog = printPushMsgOnBuildLog;
	}

	public boolean isPrintPushMsgOnBuildLog() {
		return this.config.printPushMsgOnBuildLog;
	}

	/**
	 * 获取配置，不用于保存
	 * 
	 * @return
	 */
	public NotificationConfig getUnsaveConfig() {
		NotificationConfig unsaveConfig = new NotificationConfig();

		unsaveConfig.webhookUrl = this.config.webhookUrl;
		unsaveConfig.mentionedId = this.config.mentionedId;
		unsaveConfig.mentionedMobile = this.config.mentionedMobile;
		unsaveConfig.topicName = this.config.topicName;

		unsaveConfig.useProxy = this.config.useProxy;
		unsaveConfig.proxyHost = this.config.proxyHost;
		unsaveConfig.proxyPort = this.config.proxyPort;
		unsaveConfig.proxyUsername = this.config.proxyUsername;
		unsaveConfig.proxyPassword = this.config.proxyPassword;

		unsaveConfig.failNotify = this.config.failNotify;
		unsaveConfig.failSend = this.config.failSend;
		unsaveConfig.abortSend = this.config.abortSend;
		unsaveConfig.successSend = this.config.successSend;
		unsaveConfig.unstableSend = this.config.unstableSend;
		unsaveConfig.startBuild = this.config.startBuild;
		unsaveConfig.printPushMsgOnBuildLog = this.config.printPushMsgOnBuildLog;

		return unsaveConfig;
	}

	/**
	 * 测试代理连接
	 * 
	 * @param proxyHost
	 * @param proxyPort
	 * @param proxyUsername
	 * @param proxyPassword
	 * @return
	 */
	public FormValidation doTestProxy(@QueryParameter String proxyHost, @QueryParameter int proxyPort,
			@QueryParameter String proxyUsername, @QueryParameter String proxyPassword) {
		String TEST_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0";

		if (StringUtils.isEmpty(proxyHost)) {
			return FormValidation.error("服务器不能为空");
		}

		NotificationConfig buildConfig = new NotificationConfig();
		buildConfig.useProxy = true;
		buildConfig.proxyHost = proxyHost;
		buildConfig.proxyPort = proxyPort;
		buildConfig.proxyUsername = proxyUsername;
		buildConfig.proxyPassword = Secret.fromString(proxyPassword);
		try {
			NotificationUtil.push(TEST_URL, "", buildConfig);
			return FormValidation.ok("测试成功");
		} catch (HttpProcessException e) {
			e.printStackTrace();
			return FormValidation.error("连接异常" + e.getMessage());
		}
	}

	@Override
	public boolean isApplicable(Class<? extends AbstractProject> aClass) {
		return true;
	}

	@Override
	public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
		config.webhookUrl = json.getString("webhookUrl");
		config.topicName = json.getString("topicName");
		config.mentionedId = json.getString("mentionedId");
		config.mentionedMobile = json.getString("mentionedMobile");
		config.useProxy = json.get("useProxy") != null;
		if (config.useProxy && json.get("useProxy") instanceof JSONObject) {
			JSONObject jsonObject = json.getJSONObject("useProxy");
			config.proxyHost = jsonObject.getString("proxyHost");
			config.proxyPort = jsonObject.getInt("proxyPort");
			config.proxyUsername = jsonObject.getString("proxyUsername");
			config.proxyPassword = Secret.fromString(jsonObject.getString("proxyPassword"));
		}
		save();
		return super.configure(req, json);
	}

	@Override
	public String getDisplayName() {
		return "企业微信通知";
	}

	@Override
	public String getHelpFile() {
		return "/plugin/qy-wechat-notification/help.html";
	}

}
